#
# @lc app=leetcode.cn id=783 lang=python3
#
# [783] 二叉搜索树节点最小距离
#

# @lc code=start
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def minDiffInBST(self, root: TreeNode) -> int:
        import sys
        ans = [-sys.maxsize, sys.maxsize]
        def dfs(root):
            if not root:
                return
            dfs(root.left)
            ans[1] = min(root.val-ans[0], ans[1])
            # print(ans)
            ans[0]=root.val
            dfs(root.right)
        dfs(root)
        return ans[1]
# @lc code=end

